vars <- cbind(c("connectionHc", "outsider", "futureHc", "isolation"),
              c("employment3cat", "income", "expenses", "emplSatisfied"),
              c("contactsInUsCat2", "dinner", "maxOrg", "favors"),
              c("seeDoctor", "findJobs", "legalProblems", "AdDTM"),
              c("read", "speak", "write", "listen"),
              c("polUnderstanding", "polDiscuss", "polQuiz", "polAction")
)
dimNames <- c("Psychological Integration", "Economic Integration", 
              "Social Integration", "Navigational Integration",
              "Linguistic Integration", "Political Integration")
dimShort <- c("psy", "econ", "soc", "nav", "ling", "pol")

for (i in 1:6){
  poly <- polychoric(subset(df, select = c(vars[, i])), correct = 0.5)
  pca <- principal(poly$rho, rotate = "none")
  scr <- data.frame(eigenvalues = pca$values,
                    component = c(1:length(pca$values)))
  
  screePlot <- ggplot(scr, aes(x = component, y = eigenvalues)) +
    geom_line(size = .5, colour = '#8da0cb') +
    geom_point(colour = '#8da0cb') +
    geom_hline(yintercept = 1, colour = '#fc8d62') +
    coord_cartesian(ylim = c(0, 4)) +
    xlab("") +
    ylab("") +
    ggtitle(dimNames[i]) +
    theme_minimal() +
    theme(plot.title = element_text(size = 10))
  assign(paste("scree_", dimShort[i], sep = ""), screePlot)
}

screeAr <- arrangeGrob(scree_ling, scree_psy, scree_soc, scree_econ, scree_pol, scree_nav, left = "Eigenvalue", bottom = "Component Number", nrow = 2)

# grid.arrange(scree_ling, scree_psy, scree_soc, scree_econ, scree_pol, scree_nav, left = "Eigenvalue", bottom = "Component Number", nrow = 2)
ggsave(here::here("Draft/PNAS/draft/plots", "scree.pdf"), plot = screeAr, width = 7, height = 8)
